package com.xqx.mapper;

import com.xqx.pojo.Student;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

/**
 * @author：xingquanxiang createTime：2019/10/26 14:51
 * description:
 */
public interface StudentMapper {
    class StudentSqlProvide{
        public String delSql(Map map){
            return new SQL(){{
                DELETE_FROM("ssmstudent");
                long[] array = (long[])map.get("array");
                StringBuilder sb = new StringBuilder();
                sb.append(" id in(");
                for (int i = 0; i < array.length; i++) {
                    sb.append(array[i]);
                    if (i!=array.length-1){
                        sb.append(",");
                    }
                }
                sb.append(")");
                WHERE(sb.toString());
            }}.toString();
        }

    }

    @Select("select * from ssmstudent")
    List<Student> findAll();

    @Insert("insert into ssmstudent(name,password,age,sex,birthday,address) " +
            "values(#{name},#{password},#{age},#{sex},#{birthday},#{address})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void add(Student student);

    @Update("update ssmstudent set name=#{name}, password=#{password}, age=#{age}, sex=#{sex}," +
            "birthday=#{birthday}, address=#{address} where id=#{id}")
    int edit(Student student);

    @DeleteProvider(type = StudentSqlProvide.class, method = "delSql")
    int del(long[] id);

    @Select("select * from ssmstudent where id = #{id}")
    Student findById(long id);

    @Select("select * from ssmstudent where name = #{name} and password=#{password}")
    Student findByNameAndPassword(Student student);
}
